package Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Vector;

import javax.sql.rowset.CachedRowSet;

import NetWork.NetWorkDelegate;
import NetWork.SocketConnector;
import NetWork.TransmissionUtil;

public class TransmissionTest implements NetWorkDelegate
{
	static int	totalSize	= 0;

	public static void main(String[] args) throws IOException
	{
		// TODO Auto-generated method stub
		TransmissionUtil transmission = TransmissionUtil.getSharedTransmissionUtil();
		TransmissionTest test = new TransmissionTest();
		transmission.delegateVector.add(test);
		// BufferedReader bf = new BufferedReader(new InputStreamReader(new
		// FileInputStream(new
		// File("F:/Visual Studio 2005/Projects/Test/Test/test.txt"))));
		String string = null;
		Vector<String> vec = new Vector<String>();
		// while((string = bf.readLine())!=null)
		{
			// vec.add(string);
		}
		SocketConnector connector = transmission.getConnectorByAddr("192.168.0.163", 3721);
		// for(int i=0;i<20;i++)
		{
			// connector.sendObject(vec, 0);
		}
		String inputString;
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
		while (true)
		{
			inputString = bufferedReader.readLine();
			TransmissionUtil.getSharedTransmissionUtil().executeQuery("192.168.0.163:3721", "192.168.0.160:3721", inputString);
		}
		// connector.sendFile("C:\\pic.rar");
	}

	public void didReceiveFile(int siteType, String host, int port, String fileName)
	{
		System.out.printf("recive File from %s:%d-->%s\n", host, port, fileName);
	}

	public void didReceiveObject(int siteType, String host, int port, Object obj)
	{
		System.out.printf("receive Obj from %s:%d-->%s\n", host, port, obj.getClass().toString());
		Vector<String> vector = (Vector<String>) obj;
		totalSize += vector.size();
		System.out.printf("Vector.size=%d,received %d\n", vector.size(), totalSize);
		// System.out.println(vector.elementAt(0));
		// System.out.println(vector.elementAt(1));
		// System.out.println(vector.elementAt(2));
	}

	public void didReceiveString(int siteType, String host, int port, String strVal)
	{
		System.out.printf("receive String from %s:%d-->%s\n", host, port, strVal);
	}

	public void didReceiveImportData(int siteType, String host, int port, Vector<String> data)
	{
		System.out.println("Receive import Data");
		for (int i = 0; i < data.size(); i++)
		{
			System.out.println(data.elementAt(i));
		}
	}

	public void didReceiveResultSet(int siteType, String host, int port, CachedRowSet crs)
	{
		// if (resultSet != null)
		try
		{
			int num = crs.getMetaData().getColumnCount();
			while (crs.next())
			{
				for (int i = 1; i < num; i++)
				{
					System.out.printf("%s\t", crs.getString(i));
				}
				System.out.printf("%s\n", crs.getString(num));
			}
		}
		catch (Exception e)
		{
			// TODO: handle exception
		}
	}
}
